package com.example.beautymakeup.controller;


import com.example.beautymakeup.domain.Code;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.domain.Result;
import com.example.beautymakeup.pojo.Banner;
import com.example.beautymakeup.pojo.Hot;
import com.example.beautymakeup.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @GetMapping
    public Result get(@RequestHeader("Shop_token") String token) throws Exception {
        PageBean<Banner> pageBean = bannerService.get(token);
        if (pageBean != null){
            return new Result(Code.GET_OK,"获取成功",pageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");

    }

    @GetMapping("/all")
    public Result getAll(@RequestHeader("Shop_token") String token, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Banner> all = bannerService.getAll(token, page, size);
        if (all != null){
            return new Result(Code.GET_OK,"获取成功",all);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @PutMapping("/update")
    public Result update(@RequestHeader("Shop_token") String token,@RequestBody Banner banner) throws Exception {
        boolean update = bannerService.update(token, banner);
        if (update){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }

    @PostMapping
    public Result add(@RequestHeader("Shop_token") String token,@RequestBody Banner banner) throws Exception {
        boolean add = bannerService.add(token, banner);
        if (add){
            return new Result(Code.SAVE_OK,"增加成功");
        }
        return new Result(Code.SAVE_ERR,"增加失败");
    }


    @PutMapping("/ban")
    public Result ban(@RequestHeader("Shop_token") String token,@RequestBody Banner banner) throws Exception {
        boolean ban = bannerService.ban(token, banner);
        if (ban){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }

    @DeleteMapping
    public Result delete(@RequestHeader("Shop_token") String token,@RequestBody Banner banner) throws Exception {
        boolean delete = bannerService.delete(token, banner);
        if (delete){
            return new Result(Code.DELETE_OK,"删除成功");
        }
        return new Result(Code.DELETE_ERR,"删除失败");
    }
}
